package com.iris.live.services.report.sources;


import com.iris.live.services.common.StringUtils;
import com.iris.live.services.data.models.ReportConfig;
import com.iris.live.services.report.data.BaseReportDataSource;
import com.iris.live.services.report.data.ReportContext;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2017/8/29.
 */
@Component("InsDefeatedResonRptDataSource")
public class InsDefeatedResonRptDataSource extends BaseReportDataSource{

    @Override
    public List getData(ReportContext context, List<ReportConfig> config) throws Exception {
        Map info= context.getParams() ;
        String lineType=info.get("displayType").toString();
        List<Map> datas =new ArrayList<>();
        if("1".equals(lineType)){
            datas=  getTimeLineData(info);
        }
        else{
            datas=  getOrgLineData(info);
        }
        int a=datas.size();
        return  datas;
    }
    //按时间  筛选条件： 年   大区  PMA  经销商
    public List getTimeLineData(Map info){
        Map map = new HashMap();
        String sql = "SELECT  " +
                " DATE_FORMAT(a.开票时间, '%m') AS date,  " +
                " COUNT(  " +
                "  DATE_FORMAT(a.开票时间, '%m')  " +
                " ) AS 数量,  " +
                " c.战败原因  " +
                " FROM  " +
                " vw_group_report_ins_defeat_info a  " +
                " LEFT JOIN lsh车型 b ON a.`车型编号` = b.车型编号  " +
                " LEFT JOIN lsh经销商表 allset ON a.`许可` = allset.`许可_BMBS`  " +
                " LEFT JOIN vw_group_report_ins_defeat_base c ON a.战败原因 = c.`新保战败原因编号`  " +
                " WHERE  " +
                " 1 = 1  " ;

        String startDay = info.get("startDay") == null ?"":info.get("startDay").toString(),
                endDay = info.get("endDay") == null ?"":info.get("endDay").toString();
        if (! StringUtils.isNullOrBlank(startDay)  && ! StringUtils.isNullOrBlank(endDay)) {
            sql = sql + " AND a.`开票时间`>=:startDay AND a.`开票时间`<=:endDay ";
            map.put("startDay",startDay);
            map.put("endDay",endDay);
        }

        String permit = info.get("permit") == null ?"":info.get("permit").toString();
        if(! StringUtils.isNullOrBlank(permit) && !"LSH".equalsIgnoreCase(permit)){
            sql= sql+" and ( allset.大区=:permit  or  allset.大区_EN=:permit    or allset.小区=:permit or allset.小区_EN=:permit or  " +
                     "     allset.CTA=:permit  or  allset.CTA_EN=:permit    or 小区_EN=:permit     or allset.许可_BMBS=:permit ) ";
            map.put("permit", permit);
        }
         sql += " GROUP BY  date ,c.`战败原因` ";
        return this.getList(sql,map);
    }

    //按地域
    public List getOrgLineData(Map info){
        Map map = new HashMap();
        String sql="SELECT  " +
                " 'DCH' 集团,  " +
                " allset.许可_LSH,  " +
                " allset.大区 AS 大区,  " +
                " allset.大区_EN AS 大区_EN,  " +
                " allset.小区 AS 小区,  " +
                " allset.小区_EN AS 小区_EN,  " +
                " IF (  " +
                " isnull(allset.CTA)  " +
                " OR allset.CTA = '',  " +
                " '其他cta',  " +
                " allset.CTA  " +
                " ) AS CTA,  " +
                " IF (  " +
                " isnull(allset.CTA_EN)  " +
                " OR allset.CTA_EN = '',  " +
                " 'Other CTA',  " +
                " allset.CTA_EN  " +
                " ) AS CTA_EN,  " +
                " allset.许可_BMBS AS 许可_BMBS,  " +
                " allset.经销商名 AS 经销商名,  " +
                " allset.经销商名_EN AS 经销商名_EN,  " +
                " allset.SalesCode AS salesCode,  " +
                " COUNT(allset.许可_LSH) AS 数量,  " +
                " b.`品牌`,  " +
                " c.战败原因,  " +
                " c.`状态`,  " +
                " a.`开票时间`  " +
                " FROM  " +
                    " vw_group_report_ins_defeat_info a  " +
                " JOIN lsh车型 b ON a.`车型编号` = b.车型编号  " +
                " JOIN lsh经销商表 allset ON a.`许可` = allset.`许可_BMBS`  " +
                " JOIN vw_group_report_ins_defeat_base c ON a.战败原因 = c.`新保战败原因编号`  " +
                " WHERE  " +
                " 1 = 1  ";

        String startDay = info.get("startDay") == null ?"":info.get("startDay").toString(),
                endDay = info.get("endDay") == null ?"":info.get("endDay").toString();
        if (! StringUtils.isNullOrBlank(startDay)  && ! StringUtils.isNullOrBlank(endDay)) {
            sql = sql + " AND a.`开票时间`>=:startDay AND a.`开票时间`<=:endDay ";
            map.put("startDay",startDay);
            map.put("endDay",endDay);
        }

        String permit = info.get("permit") == null ?"":info.get("permit").toString();
        if(! StringUtils.isNullOrBlank(permit) && !"LSH".equalsIgnoreCase(permit)){
            sql= sql+" and ( allset.大区=:permit  or  allset.大区_EN=:permit    or allset.小区=:permit or allset.小区_EN=:permit or  " +
                    "     allset.CTA=:permit  or  allset.CTA_EN=:permit    or 小区_EN=:permit     or allset.许可_BMBS=:permit ) ";
            map.put("permit", permit);
        }
        sql = sql +" GROUP BY  c.战败原因, 许可_BMBS ";
        return this.getList(sql,map);
    }


}
